
/*加载页面获取一级分类*/
$.ajax({
    url:"getCategroy",
    data:{"type":"", "categroy":""},
    type:"post",
    dataType:"json",
    success:function (val) {
        var str="";
        val=JSON.parse(val);
        for(var i=0;i<val.length;i++){
            var cur=val[i];
            str+=`<option name="${cur.id}"  value="${cur.categroy_name}">${cur.categroy_name}</option>`;
        }
        $("#firstclass").after(str);
    }
});
/*列表改变获取对应下级分类*/
$("#first").change(getsection);
$("#section").change(getthree);
function getthree(){
    if($("#section>option:selected").index()!==0){
        $("#text2").hide();
        $("#btn2").hide();
        $("#threewrapper").removeClass("hide");
        var categroy1= $("#section>option:selected").attr("name");
        $("#categroy").val(categroy1)
    }else {
        $("#text2").show();
        $("#btn2").show();
        $("#threewrapper").addClass("hide")
    }
}
/*获取二级分类*/
$(document).on("click","#section",function(){
    getthree()
});
function getsection() {
        if($("#first>option:selected").index()!==0){
            $("#text1").hide();
            $("#btn1").hide();
            $("#secondwrapper").removeClass('hide');
            var selectoption= $("#first option:selected");
            var id=selectoption.attr("name");
            $.ajax({
                url:"getCategroy",
                data:{"type":1, "categroy":id},
                type:"post",
                dataType:"json",
                success:function (val) {
                    var str=`<option value="" id="sectionclass">请选择二级分类</option>`;
                    val=JSON.parse(val);
                    for(var i=0;i<val.length;i++){
                        var cur=val[i];
                        str+=`<option name="${cur.id}"  value="${cur.categroy_name}">${cur.categroy_name}</option>`;
                    }

                    $("#section").html("");
                    $("#section").html(str);
                }
            })
        }else {
            $("#text1").show();
            $("#btn1").show();
            $("#secondwrapper").addClass("hide")
            $("#threewrapper").addClass("hide")
        }
}
/*新增一级*/
$("#addfirst").click(function () {
   var value= $("#text1").find("input").val();
        value= value.replace(/\s/g,"");
    if(value){
        $.ajax({
            url:"insertCategroy",
            data:{"type":1,"categroy":"0","categroy_name":value},
            type:"post",
            async:false,
            success:function (val) {
                console.log(val);

                if(val.id){
                    var cur=val;
                    var str=`<option name="${cur.id}"  value="${value}" selected>${value}</option>`;
                    $("#firstclass").after(str);
                    getsection();
                    iosOverlay({
                        text: "添加成功!",
                        duration: 2e3,
                        icon: "../../images/check.png"
                    });
                } else if(val.code==100){
                    iosOverlay({
                        text: "添加失败!",
                        duration: 2e3,
                        icon: "../../images/cross.png"
                    });
                }else if(val.code==300){
                    iosOverlay({
                        text: "该分类名已存在!",
                        duration: 2e3,
                        icon: "../../images/cross.png"
                    });
                }

            }
        });
        $("#text1").find("input").val("");
    }else{
        iosOverlay({
            text: "请填写分类名称",
            duration: 2e3,
            icon: "../../images/cross.png"
        });
    }
});
$("#type").val(3);
/*新增二级*/
$("#addsection").click(
    function () {
        var value= $("#text2").find("input").val();
        value= value.replace(/\s/g,"");
        var categroy=$("#first>option:selected").attr("name");
        console.log(categroy,value);
        var id=$("#first>option:selected").attr("id")==="firstclass"?0:1;
        if(value&&id) {
            $.ajax({
                url: "insertCategroy",
                data: {"type":2, "categroy": categroy, "categroy_name": value},
                type: "post",
                async: false,
                success: function (val) {

                    if(val.id){
                        var cur = val;
                        var str = `<option name="${cur.id}"  value="${value}" selected>${value}</option>`;
                        $("#sectionclass").after(str);
                        getthree()
                        iosOverlay({
                            text: "添加成功!",
                            duration: 2e3,
                            icon: "../../images/check.png"
                        });
                    }else if(val.code==100){
                        iosOverlay({
                            text: "添加失败!",
                            duration: 2e3,
                            icon: "../../images/cross.png"
                        });
                    }else if(val.code==300){
                        iosOverlay({
                            text: "该分类名已存在!",
                            duration: 2e3,
                            icon: "../../images/cross.png"
                        });
                    }

                }
            });
            // window.location.reload();
            $("#text2").find("input").val("");
        }
    }
);
/*上传图片预览效果*/
$(function () {
    $("#up").uploadPreview({ Img: "ImgPr", Width: 390, Height: 390,size:300 });
});

/*新增三级分类*/
$("#addthree").click(function (){
     var textthree= $("#text3").val();
     var imgvalue=$("input[type=file]").val();
    if(!textthree ){
        iosOverlay({
            text: "请填写三级分类!",
            duration: 2e3,
            icon: "../../images/cross.png"
        });
        return false
    }else if(!imgvalue){
        iosOverlay({
            text: "请选择图片",
            duration: 2e3,
            icon: "../../images/cross.png"
        });
        return false
    }
});
